/* ============================================================================= */
/*                        TABLE 5 - REJECTED LOANS ANALYSIS                      */
/* ============================================================================= */

clear all
		
capture log close
log using "$logs\table_5_rejected.log", replace

capture estimates drop *

/* ------------------------------------------------------------------------------
   Step 1: Loan Types Decomposition - Long-Term vs. Other Loans
------------------------------------------------------------------------------ */

use "$output\rejected_psm.dta", clear

    keep if mdate < mofd(date("20200101","YMD"))
    keep if mdate > mofd(date("20131231","YMD"))
    keep if deltamonths >= -12 & deltamonths <= 24

    * Compute loan logs
    gen lltloan = log(ltloan + bm + bi)   // Long-term loans
    gen lother = log(stloan + oc)         // Other loans
    gen lloan = log(ltloan + bm + bi + stloan + oc) // Total loans

    * Long-term loan regression
    qui eststo regltloan: reghdfe lltloan i1.accepted##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren) 
    estadd local yearfirm "Y", replace   
    estadd local mdateFE "Y", replace  

    * Other loan regression
    qui eststo reglother: reghdfe lother i1.accepted##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren) 
    estadd local yearfirm "Y", replace   
    estadd local mdateFE "Y", replace  

/* ------------------------------------------------------------------------------
   Step 2: Investment Loans Only
------------------------------------------------------------------------------ */

use "$output\rejected_psm_forinvestment1.dta", clear

    keep if mdate < mofd(date("20200101","YMD"))
    keep if mdate > mofd(date("20131231","YMD"))
    keep if deltamonths >= -12 & deltamonths <= 24

    gen loan = stloan + ltloan + bm + bi + oc
    sort siren mdate
    gen lloan = log(loan)

    * Investment loan regression
    eststo reginvest: reghdfe lloan i1.accepted##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren)
    estadd local yearfirm "Y", replace 
    estadd local mdateFE "Y", replace  

/* ------------------------------------------------------------------------------
   Step 3: Non-Investment Loans
------------------------------------------------------------------------------ */

use "$output\rejected_psm_forinvestment0.dta", clear

    keep if mdate < mofd(date("20200101","YMD"))
    keep if mdate > mofd(date("20131231","YMD"))
    keep if deltamonths >= -12 & deltamonths <= 24

    gen loan = stloan + ltloan + bm + bi + oc
    sort siren mdate
    gen lloan = log(loan)

    * Non-investment loan regression
    eststo regnotinvest: reghdfe lloan i1.accepted##i1.post [pweight=weight], ///
        a(i.siren#i.year mdate) cluster(siren)
    estadd local yearfirm "Y", replace 
    estadd local mdateFE "Y", replace  

/* ============================================================================= */
/*               EXPORT TABLE 5b TO LaTeX                              */
/* ============================================================================= */

esttab regltloan reglother reginvest regnotinvest using "$tables\Table_5_rejected_final.tex", replace ///
    noomitted nobase fragment gap booktabs label nonote noobs nolines /// 
    b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) /// 
    mtitles("Long-term loans" "Other loans" "For investment" "Not for investment") ///
    order(1.accepted#1.post 1.post) ///
    varlabel(1.post "Post" 1.accepted#1.post `"Accepted $\times$ Post"') ///
    stats(yearfirm mdateFE N r2, ///
        label(`"Firm-Year FE"' `"Month FE"' `"\midrule N"' `"R-sq"') ///
        layout("\multicolumn{1}c{@}") ///
        fmt(%9.0fc %9.0fc %9.0fc %9.2f))

log close
